package com.sx.controller;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;
import com.dfec.model.Msg;
import com.dfec.utils.Base64Coder;
import com.dfec.utils.Consists;
import com.dfec.utils.ResponseUtil;
import com.sx.service.SpecialWeatherService;

/**
 * Servlet implementation class UserDataServlet
 */
@WebServlet(name = "sws", description = "特殊天气现象", urlPatterns = { "/sws" })
public class SpecialWeatherServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private List<Object> list=null;
    Msg msg=null;
	SpecialWeatherService s=new SpecialWeatherService();
	
	@SuppressWarnings("unchecked")
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		msg=new Msg();
		list=new ArrayList<>();
		
		String type = request.getParameter("type");
		try {
			if (type.equals("save")) {
				String details=request.getParameter("details");
				String phone=request.getParameter("phone");
				String title=request.getParameter("title");
				String url=request.getParameter("url");
				
				int i=s.upLoad(details, phone,url,title);
				Map map=new HashMap<>();
				if(i>0){
					msg.setCode(Consists.DATA_Code_OK);
					msg.setMsg(Consists.DATA_Msg_OK);
				}else {
					msg.setCode(Consists.DATA_Code_OK__NODATA);
					msg.setMsg(Consists.DATA_Msg_OK_ONDATA);
				}
				list.add(msg);
				list.add(map);
				
			}else if(type.equals("list")){
				String position=request.getParameter("position");
				int page=Integer.parseInt(request.getParameter("page"));
				int size=Integer.parseInt(request.getParameter("size"));
				List<Object> getdata = s.getSpecialWeather(position, page, size);
				if(!getdata.get(0).toString().equals("[]")){
					msg.setCode(Consists.DATA_Code_OK);
					msg.setMsg(Consists.DATA_Msg_OK);
					
				}else {
					msg.setCode(Consists.DATA_Code_OK__NODATA);
					msg.setMsg(Consists.DATA_Msg_OK_ONDATA);
				}
				list.add(msg);
				list.add(getdata.get(0));
				
			}else if (type.equals("search")) {
				String keyword=request.getParameter("keyword");
				String position=request.getParameter("position");
				List<Object> getdata = s.searchSpecialWeather(keyword,position);
				if(!getdata.get(0).toString().equals("[]")){
					msg.setCode(Consists.DATA_Code_OK);
					msg.setMsg(Consists.DATA_Msg_OK);
				}else {
					msg.setCode(Consists.DATA_Code_OK__NODATA);
					msg.setMsg(Consists.DATA_Msg_OK_ONDATA);
				}
				list.add(msg);
				list.add(getdata.get(0));
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			msg.setCode(Consists.Error_Code_ServerError);
			msg.setMsg(Consists.Error_Msg_ServerError);
			list.add(msg);
			list.add(new ArrayList<>());
			
		}
		 ResponseUtil.sendScript(response, JSON.toJSONString(list));
		
	}

}
